#include "EnemyPool.h"

EnemyPool::EnemyPool()
	: mProjectileEnemyIndex (0)
	, mMeleeEnemyIndex (0)
	, mEnemySpawnDelay (0.0f)
	, kMaxEnemies (100)
{}

EnemyPool::~EnemyPool()
{
}

void EnemyPool::Load(int level)
{
	///////PROJECTILE ENEMIES//////
	for(int i=0; i<kMaxEnemies; ++i)
		mProjectileEnemies[i].Load();

	///////MELEE ENEMIES///////
	for(int i=0; i<kMaxEnemies; ++i)
		mMeleeEnemies[i].Load();

	if(level == 1)
		SpawnLevel1Enemies();
	else if(level == 2)
		SpawnLevel2Enemies();
}

void EnemyPool::Unload()
{
	for(int i=0; i<kMaxEnemies; ++i)
		mProjectileEnemies[i].Unload();

	for(int i=0; i<kMaxEnemies; ++i)
		mMeleeEnemies[i].Unload();
}

void EnemyPool::Update(float deltaTime, Map& map, Character& character)
{
	for(int i=0; i<kMaxEnemies; ++i)
		mProjectileEnemies[i].Update(deltaTime, map, character);

	for(int i=0; i<kMaxEnemies; ++i)
		mMeleeEnemies[i].Update(deltaTime, map, character);
}

void EnemyPool::Render(const SVector2& viewOffset)
{
	for(int i=0; i<kMaxEnemies; ++i)
		mProjectileEnemies[i].Render(viewOffset);

	for(int i=0; i<kMaxEnemies; ++i)
		mMeleeEnemies[i].Render(viewOffset);
}

void EnemyPool::SpawnEnemies(float deltaTime)
{
	mEnemySpawnDelay -= deltaTime;

	if(mEnemySpawnDelay <= 0)
	{
		if(!mProjectileEnemies[mProjectileEnemyIndex].isActive())
		{
			//SPAWN ENEMIES
			const int kWinWidth = IniFile_GetInt("WinWidth", 800);
			const float posX = kWinWidth * RandomFloat(0.1f, 0.9f);
			const float posY = 300.0f;

			SVector2 pos (posX, posY);

			mProjectileEnemies[mProjectileEnemyIndex].Spawn(pos, SVector2(0.0f, 200.0f), SVector2(-500.0f, 0.0f));
			mProjectileEnemyIndex = (mProjectileEnemyIndex + 1) % kMaxEnemies;
		}

		if(!mMeleeEnemies[mMeleeEnemyIndex].isActive())
		{
			const int kWinWidth = IniFile_GetInt("WinWidth", 800);
			const float posX = kWinWidth * RandomFloat(0.1f,0.9f);
			const float posY = 400.0f;
			
			SVector2 pos(posX, posY);
			mMeleeEnemies[mMeleeEnemyIndex].Spawn(pos);
			mMeleeEnemyIndex = (mMeleeEnemyIndex + 1) % kMaxEnemies;
		}

		mEnemySpawnDelay = RandomFloat(0.5f, 2.0f);
	}
}

void EnemyPool::SpawnLevel1Enemies()
{
	SVector2 horizontalVelocity(200.0f, 0.0f);
	SVector2 verticalVelocity(0.0f, 200.0f);

	SVector2 projectileUp(0.0f, -500.0f);
	SVector2 projectileDown(0.0f, 500.0f);
	SVector2 projectileLeft(-500.0f, 0.0f);
	SVector2 projectileRight(500.0f, 0.0f);

	mProjectileEnemies[0].Spawn(SVector2(3700.0f, 400.0f), verticalVelocity, projectileLeft);
	mProjectileEnemies[1].Spawn(SVector2(1900.0f, 400.0f), verticalVelocity, projectileLeft);
	mProjectileEnemies[2].Spawn(SVector2(1000.0f, 700.0f), horizontalVelocity, projectileUp);
	mProjectileEnemies[3].Spawn(SVector2(2500.0f, 300.0f), horizontalVelocity, projectileDown);
	mProjectileEnemies[4].Spawn(SVector2(3000.0f, 300.0f), horizontalVelocity, projectileDown);
	
	mMeleeEnemies[0].Spawn(SVector2(1500.0f, 200.0f));
	mMeleeEnemies[1].Spawn(SVector2(2250.0f, 700.0f));
	mMeleeEnemies[2].Spawn(SVector2(3700.0f, 700.0f));
	mMeleeEnemies[3].Spawn(SVector2(3750.0f, 650.0f));
}

void EnemyPool::SpawnLevel2Enemies()
{
	SVector2 horizontalVelocity(200.0f, 0.0f);
	SVector2 verticalVelocity(0.0f, 200.0f);

	SVector2 projectileUp(0.0f, -500.0f);
	SVector2 projectileDown(0.0f, 500.0f);
	SVector2 projectileLeft(-500.0f, 0.0f);
	SVector2 projectileRight(500.0f, 0.0f);

	mProjectileEnemies[0].Spawn(SVector2(0.0f, 100.0f), horizontalVelocity, projectileDown);
	mProjectileEnemies[1].Spawn(SVector2(100.0f, 100.0f), horizontalVelocity, projectileDown);
	mProjectileEnemies[2].Spawn(SVector2(200.0f, 100.0f), horizontalVelocity, projectileDown);
	mProjectileEnemies[3].Spawn(SVector2(750.0f, 350.0f), horizontalVelocity, projectileUp);
	mProjectileEnemies[4].Spawn(SVector2(850.0f, 350.0f), horizontalVelocity, projectileUp);
	mProjectileEnemies[5].Spawn(SVector2(950.0f, 350.0f), horizontalVelocity, projectileUp);
	mProjectileEnemies[6].Spawn(SVector2(925.0f, 1000.0f), verticalVelocity, projectileLeft);
	mProjectileEnemies[7].Spawn(SVector2(100.0f, 1400.0f), verticalVelocity, projectileRight);

	mMeleeEnemies[0].Spawn(SVector2(925.0f, 1700.0f));
	mMeleeEnemies[1].Spawn(SVector2(700.0f, 1400.0f));
	mMeleeEnemies[2].Spawn(SVector2(750.0f, 1350.0f));
	mMeleeEnemies[4].Spawn(SVector2(100.0f, 800.0f));
}